package nsu.edu.zsq.service.impl.sys;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import nsu.edu.zsq.bean.Message;
import nsu.edu.zsq.bean.MessageExample;
import nsu.edu.zsq.common.RequestHolder;
import nsu.edu.zsq.dao.MessageMapper;
import nsu.edu.zsq.exception.ParamException;
import nsu.edu.zsq.param.PageQuery;
import nsu.edu.zsq.service.sys.MessageService;
import nsu.edu.zsq.service.sys.SysLogService;
import nsu.edu.zsq.util.BeanValidator;

/** 
* Description: 学校通知的service实现类<br>
*/
@Service
public class MessageServiceImpl implements MessageService {

    @Autowired
    private MessageMapper messageMapper;
    @Autowired
    private SysLogService sysLogService;
    
    @Override
    public Message getMessage(int messageId) {
        Message message = messageMapper.selectByPrimaryKey(messageId);
        return message;
    }
    
    @Override
    public Message getTopMessage() {
        MessageExample example = new MessageExample();
        example.createCriteria().andTopEqualTo(1);
        List<Message> listMessage = messageMapper.selectByExampleWithBLOBs(example);
        if (listMessage != null && listMessage.size() > 0) {
            return listMessage.get(0);
        } else {
            return new Message();
        }
    }
    
    @Override
    public PageInfo<Message> listMessageWithPage(PageQuery pageQuery) {
        PageHelper.startPage(pageQuery.getPageNum(),pageQuery.getPageSize());
        MessageExample example = new MessageExample();
        example.setOrderByClause("top desc");
        List<Message> listMessage = messageMapper.selectByExampleWithBLOBs(example);
        PageInfo<Message> pageInfo = new PageInfo<Message>(listMessage, pageQuery.getNavigatePages());
        return pageInfo;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveMessage(Message message) {
        BeanValidator.check(message);
        message.setOperator(RequestHolder.getCurrentSysUser().getAccount());
        message.setOperatorTime(new Date());
        // 检查是否存在置顶消息
        if (message.getTop() == 1 && checkTopExist()) {
            throw new ParamException("置顶消息最多一条");
        }
        messageMapper.insertSelective(message);
        
        // 添加日志
        String instruction = "学校通知管理-发布通知";
        sysLogService.saveSysLogBySysUser(null, message, instruction);
    }

    @Override
    public void updateMessage(Message message) {
        BeanValidator.check(message);
        Message before = getMessage(message.getId());
        message.setOperator(RequestHolder.getCurrentSysUser().getAccount());
        message.setOperatorTime(new Date());
        // 检查是否存在置顶消息
        if (message.getTop() == 1 && checkTopExist()) {
            throw new ParamException("置顶消息最多一条");
        }
        messageMapper.updateByPrimaryKeyWithBLOBs(message);
        
        // 添加日志
        String instruction = "学校通知管理-编辑";
        sysLogService.saveSysLogBySysUser(before, message, instruction);
    }
    
    /**
     * 检查是否已经存在置顶消息
     * @return boolean true：存在 false：不存在
     */
    private boolean checkTopExist() {
        MessageExample example = new MessageExample();
        example.createCriteria().andTopEqualTo(1);
        return messageMapper.countByExample(example) > 0;
    }

    @Override
    public void deleteMessage(int[] messageIds) {
        List<Message> before = new ArrayList<Message>();
        for (int messageId : messageIds) {
            Message message = getMessage(messageId);
            before.add(message);
            messageMapper.deleteByPrimaryKey(messageId);
        }

        // 添加日志
        String instruction = "学校通知管理-删除";
        sysLogService.saveSysLogBySysUser(before, null, instruction);
    }


}
